package ControlLayer;
import java.util.ArrayList;
import ModelLayer.*;
//model layer package has to be imported
/**
 * Controller class for the DVD collection
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * @revised version English (Ab)
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class DVDCtr
{
    //declare instance variables to reference containers
    private DVDContainer content;
    private ArrayList<DVD> bodyCount;
    /**
     * Constructor for objects of class DVDCtr
     */
    public DVDCtr()
    {
        //set up references to containers  
        content = DVDContainer.getInstance();
                bodyCount = listAllDvds();
    }
    
    //Dummy implementation: after implementing the model layer 
    
    //Dummy implementation: more parameters are probaly to added
    public void createDVD(String id, String title, String artist, String date)
    {       
          //this method constructs a new dvd object 
          //and stores it in the container
          
          
          content.createDVD(id, title, artist, date);
          bodyCount = listAllDvds();
    }
    
   
    public DVD findDVD( String no)
    {
        //this method is to search the container for a DVD
        //with an id-number equal to the parameter no
        //when the dvd is found, the method is to call print-methods in the TUI layer
        boolean ok=false;
        bodyCount = listAllDvds();

        int x=0;
        int a;
        a = bodyCount.size();
        while(x<a && !ok)
        {
            DVD p = bodyCount.get(x);
            if(p.getID()==no)
            {
                ok=true;
            }
            else
            {
                x++;
            }
        }
        if(ok)
        {
            return bodyCount.get(x);
            
        }
        else{

            return null;
        }
   
    }
    public void deleteDVD(String no)
    {
        
        //search for the dvd and delete the object from the container
        DVD c=findDVD(no);
        if(c!=null)
        {
            bodyCount.remove(c);
        }
        bodyCount = listAllDvds();
    }
    public ArrayList<Copy> listAllCopies(String id)
    {
        bodyCount = listAllDvds();
        DVD c=findDVD(id);
        if(c!=null)
        {
            return c.getCopies();
        }
        else
        {
        return null;
       }
    }
    public Copy checkTitle(String search)
    {
        bodyCount = listAllDvds();
        DVD p = findDVD(search);
        if(p!=null)
        {
            return p.check();
        }
        else
        {return null;
        }
    }
    public void createCopy(String id, Copy newItem)
    {
        //create a copy of the specified dvd and add it to the dvd
        DVD c=findDVD(id);
        
        c.addCopy(newItem);
        bodyCount = listAllDvds();
    }
    public ArrayList<DVD> listAllDvds()
    {
          //returns a list of all dvds and their associated copies
          return content.listAllDvds();
    }
    public void createCopies(String no, int serial, double price, String date)
    {
      Copy add = new Copy(serial, date, price);
      createCopy(no, add);
      bodyCount = listAllDvds();
    }
    
    public void rentCopy(String rentTitle, long rentCopyID, String loanDate, String loanPeriod)
    {
        DVD r = findDVD(rentTitle);
        r.rentCopy(rentCopyID, loanDate, loanPeriod);
        bodyCount = listAllDvds();
    }
    public Copy noIdea(String sdvd, int specie)
    {
        DVD s = findDVD(sdvd);
        return s.findCopy(specie);
    }
}

